﻿function drawPlayMenu(ctx) {
    var dim = gamvas.getCanvasDimension();
    ctx.save();
    ctx.fillStyle = '#fff';
    ctx.font = 'bold 32px sans-serif';
    ctx.textAlign = 'center';
    ctx.fillText("Play Game", dim.w / 2, (dim.h / 2) - 67);
    ctx.restore();
}

var b2Vec2 = Box2D.Common.Math.b2Vec2;

buttonBehavior = gamvas.ActorState.extend({
    onMouseDown: function (button, x, y) {
        if (this.actor.fixture.GetShape().TestPoint(this.actor.fixture.GetBody().GetTransform(), new b2Vec2(gamvas.physics.toWorld(x), gamvas.physics.toWorld(y)))) {
            
            gamvas.state.setState('game');
        }
    },
    doCollide: function () {
        return false;
    }
});

button = gamvas.Actor.extend({
    create: function (name, x, y, w, h) {
        this._super(name, x, y);
        var st = gamvas.state.getCurrentState();
        this.type = 'button';


        var dim = gamvas.getCanvasDimension();
        
        this.setCenter(Math.round(dim.w / 2), Math.round(dim.h / 2) - 67);
        this.layer = 8;
        this.addState(new buttonBehavior('sample_behave'), true);
        this.bodyRect(this.position.x, this.position.y, w, h, gamvas.physics.STATIC);
    }
});


mainMenuState = gamvas.State.extend({
    init: function () {
        gamvas.physics.resetWorld(0, 9.81, true);
        var d = gamvas.getCanvasDimension();
        gamvas.physics.pixelsPerMeter = 128;
        // create the emitter
        this.emitter = new coinEmitter('coin');
        this.addActor(this.emitter);
        // disable screen clearing, as we draw a fullscreen image anyway
        this.autoClear = false;

        this.startButton = new button('start', gamvas.getCanvasDimension().w / 2, gamvas.getCanvasDimension().h / 2 -67, 155, 48);
        this.addActor(this.startButton);
        this.registerInputEvents(this.startButton);
        // get the background
        this.bg = new gamvas.Image(this.resource.getImage(baseURL+'Content/images/mainbg.jpg'));
    },

    preDraw: function (t) {
        // draw the background before camera is applied
        this.bg.draw(t);
    },

    postDraw: function (t) {
        // draw help after camera was applied
        drawPlayMenu(this.c);
    },

    // change to next state on space
    onKeyUp: function (k) {
        if (k == gamvas.key.SPACE) {
            gamvas.state.setState('game');
        }
        return false;
    }
});
